package name.wilbur.course.db.bigone.control;

import java.util.List;

import name.wilbur.course.db.bigone.model.BaseDBModel;
import name.wilbur.course.db.bigone.model.City;
import name.wilbur.course.db.bigone.model.Profile;
import name.wilbur.course.db.bigone.model.User;
import name.wilbur.course.db.bigone.service.IArticleService;
import name.wilbur.course.db.bigone.service.IPrepareService;
import name.wilbur.course.db.bigone.service.IUserService;
import name.wilbur.course.db.bigone.service.impl.ArticleService;
import name.wilbur.course.db.bigone.service.impl.PrepareService;
import name.wilbur.course.db.bigone.service.impl.UserService;

public class PersonalInfoMgr extends BaseMgr {
	private Profile profile;
	private User user;
	private City myCity;
	private IUserService userServ;
	private IPrepareService prepareServ;
	private IArticleService articleServ;
	
	public PersonalInfoMgr(User user) {
		this.user = user;
		userServ = new UserService();
		prepareServ = new PrepareService();
		articleServ = new ArticleService();
		
		profile = userServ.getProfile(user.getPk());
		if (profile != null)
			myCity = userServ.getUserCity(profile.getCityID());
	}
	
	public List<BaseDBModel> getArticles() {
		return articleServ.getAllArticles();
	}
	
	public Profile getProfile() {
		return profile;
	}
	
	public boolean updatePersonalInfo(String gender, String cityName, String phone) {
		boolean freshP = false;
		if (profile == null) {
			freshP = true;
			profile = new Profile();
			profile.setUserID(user.getPk());
		}
		profile.setGender(gender);
		profile.setCityID(prepareServ.getCityByCityName(cityName).getPk());
		profile.setPhoneNumber(phone);
		if (freshP)
			return userServ.createProfile(profile);
		else 
			return userServ.updateProfile(profile);
	}
	
	public String[] getAllCityNames() {
		return prepareServ.getAllCityNames();
	}
	
	public User getUser() {
		return user;
	}
	
	public City getCity() {
		return myCity;
	}
}
